package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.demo.Result;
import com.woniuxy.dto.SaveWarehouseDto;
import com.woniuxy.dto.ShopDto;
import com.woniuxy.dto.WarehouseDto;
import com.woniuxy.entity.WarehouseTable;
import com.woniuxy.param.*;
import com.woniuxy.service.WarehouseTableService;
import com.woniuxy.util.ValidatedUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 仓库表 前端控制器
 * </p>
 *
 * @author zcx,ftl,gr,rhs
 * @since 2021-12-31
 */
@RestController
@RequestMapping("/warehouse-table")
@Api(tags = "仓库接口")
public class WarehouseTableController {
    @Autowired
    private WarehouseTableService warehouseTableService;

    /**
    * @param listWareHouseParam
     * 分页查询所有仓库
     */
    @ApiOperation("分页查询所有仓库")
    @PostMapping("/listWareHouse")
    public Result listWareHouse(@RequestBody ListWareHouseParam listWareHouseParam){
        return Result.newInstance().setCode(200).setMsg("查询成功").setData(warehouseTableService.listWareHouse(listWareHouseParam));
    }


    /**
     * 查询所有正常仓库
     * @return
     */
    @ApiOperation("查询所有仓库")
    @GetMapping("/listAllhouse")
    public Result listAllhouse(){
        return Result.newInstance().setCode(200).setMsg("查询成功").setData(warehouseTableService.list(new QueryWrapper<WarehouseTable>().eq("warehouse_remove_status", 0)));
    }

    /**
     * 添加仓库
     * @param saveWarehouseParam
     */
    @ApiOperation("添加仓库")
    @PostMapping("/SaveWarehouse")
    public Result saveShop(@RequestBody  @Validated SaveWarehouseParam saveWarehouseParam, BindingResult result) {
        //参数验证
        ValidatedUtil.ValidatedException(result);
        SaveWarehouseDto saveWarehouseDto = warehouseTableService.SaveWarehouse(saveWarehouseParam);
        if (null!=saveWarehouseDto){
            return Result.newInstance().setCode(200).setMsg("添加成功").setData(saveWarehouseDto);
        }
        return Result.newInstance().setCode(500).setMsg("添加失败");
    }
    /**
     * 修改仓库
     * @param updateWarehouseParam
     * @return
     */
    @PostMapping("/updateWarehouse")
    @ApiOperation("修改仓库")
    public Result updateWarehouse(@RequestBody  @Validated UpdateWarehouseParam updateWarehouseParam, BindingResult result) {
        //参数验证
        ValidatedUtil.ValidatedException(result);
        Boolean b = warehouseTableService.updateWarehouse(updateWarehouseParam);
        if (b){
            return Result.newInstance().setCode(200).setMsg("修改成功");
        }
        return Result.newInstance().setCode(500).setMsg("修改失败");
    }
    /**
     * 删除仓库
     * @param warhouseIds
     * @return
     */
    @PostMapping("/delWarehouse")
    @ApiOperation("删除仓库")
    public Result delWarehouse(@RequestBody List<Integer> warhouseIds) {
        if ( warehouseTableService.delWarehouse(warhouseIds)){
            return Result.newInstance().setCode(200).setMsg("删除成功");
        }
        return Result.newInstance().setCode(500).setMsg("删除失败");
    }


    @GetMapping("/gethouse/{id}")
    @ApiOperation("根据Id查仓库")
    public Result getHouse(@PathVariable Integer id) {
        if (null!=warehouseTableService.getwarehouse(id)){
            return Result.newInstance().setCode(200).setMsg("查询成功").setData(warehouseTableService.getwarehouse(id));
        }
        return Result.newInstance().setCode(500).setMsg("查询失败");
    }
}

